<?php

  //include('../includes/classes/info.php');

  class osC_info_Admin {
    public static function getData($id) {
      global $osC_Database, $osC_Language;


      $QinfoListItem = $osC_Database->query('select * from :table_info where info_id = :info_id and language_id = :language_id');
      $QinfoListItem->bindTable(':table_info', TABLE_INFO);
      $QinfoListItem->bindInt(':info_id', $id);
      $QinfoListItem->bindInt(':language_id', $osC_Language->getID());
      $QinfoListItem->execute();
      
      $data = $QinfoListItem->toArray(); 
      
      $QinfoListItem->freeResult();
      
      return $data;
      
    }

    public static function save($id = null, $data, $default = false) {
      global $osC_Database, $osC_Language;

      $error = false;

      $osC_Database->startTransaction();

      if ( is_numeric($id) ) {
        $info_id = $id;
      } else {
        $Qstatus = $osC_Database->query('select max(info_id) as info_id from :table_info');
        $Qstatus->bindTable(':table_info', TABLE_INFO);
        $Qstatus->execute();

        $info_id = $Qstatus->valueInt('info_id') + 1;
      }

      foreach ( $osC_Language->getAll() as $l ) {
        if ( is_numeric($id) ) {
          $Qstatus = $osC_Database->query('update :table_info set active = :active, sort_order = :sort_order, info_name = :info_name, info_description = :info_description, last_modified = :last_modified where info_id = :info_id and language_id = :language_id');
        } else {
          $Qstatus = $osC_Database->query('insert into :table_info (info_id, language_id, info_name, info_description, active, sort_order, last_modified, date_added) values (:info_id, :language_id, :info_name, :info_description, :active, :sort_order, :last_modified, :date_added)');
        }

        $Qstatus->bindTable(':table_info', TABLE_INFO);
        $Qstatus->bindInt(':info_id', $info_id);
        $Qstatus->bindInt(':active', $data['active']);
        $Qstatus->bindInt(':sort_order', $data['sort_order']);
        $Qstatus->bindValue(':info_name', $data['info_name'][$l['id']]);
        $Qstatus->bindValue(':info_description', $data['info_description'][$l['id']]);
        $Qstatus->bindRaw(':date_added', 'now()');
        $Qstatus->bindRaw(':last_modified', 'now()');
        $Qstatus->bindInt(':language_id', $l['id']);
        
        $Qstatus->setLogging($_SESSION['module'], $info_id);
        $Qstatus->execute();

        if ( $osC_Database->isError() ) {
          $error = true;
          break;
        }
      }

      

      if ( $error === false ) {
        $osC_Database->commitTransaction();

        if ( $default === true ) {
          //osC_Cache::clear('configuration');
        }

        return true;
      }

      $osC_Database->rollbackTransaction();

      return false;
    }


    public static function delete($id, $categories = null) {
      global $osC_Database;

      $delete_product = true;
      $error = false;

      $osC_Database->startTransaction();

        $Qpc = $osC_Database->query('delete from :table_info where info_id = :info_id');
        $Qpc->bindTable(':table_info', TABLE_INFO);
        $Qpc->bindInt(':info_id', $id);
        $Qpc->setLogging($_SESSION['module'], $id);
        $Qpc->execute();


      if ( $error === false ) {
        $osC_Database->commitTransaction();

        return true;
      }

      $osC_Database->rollbackTransaction();

      return false;
    }
    
    
    public static function xsell_save_sort($data, $default = false) {
      global $osC_Database, $osC_Language;

      $error = false;

      $osC_Database->startTransaction();

            $Xupdate = $osC_Database->query('UPDATE :table_info_xsell SET sort_order = :sort_value WHERE xsell_id= :xsell_id and info_id = :info_id');
            $Xupdate->bindTable(':table_info_xsell', TABLE_INFO_XSELL);
            $Xupdate->bindInt(':sort_value', $data['short_order']);
            $Xupdate->bindInt(':xsell_id', $data['xsell_id']);
            $Xupdate->bindInt(':info_id', $data['info_id']);            
            $Xupdate->execute();
       
        if ( $osC_Database->isError() ) {
          $error = true;
          break;
        }
    
     

      if ( $error === false ) {
        $osC_Database->commitTransaction();

        if ( $default === true ) {
          //osC_Cache::clear('configuration');
        }

        return true;
      }

      $osC_Database->rollbackTransaction();

      return false;
    }
    
    public static function category_tree($id = null, $categories = null) {
      global $osC_Database, $osC_Language;

      $Qcategories = $osC_Database->query('select c.categories_id, c.parent_id, c.categories_image, cd.categories_name from :table_categories c, :table_categories_description cd where c.categories_id = cd.categories_id and cd.language_id = :language_id order by c.parent_id, c.sort_order, cd.categories_name');
      $Qcategories->bindTable(':table_categories', TABLE_CATEGORIES);
      $Qcategories->bindTable(':table_categories_description', TABLE_CATEGORIES_DESCRIPTION);
      $Qcategories->bindInt(':language_id', $osC_Language->getID());
      $Qcategories->execute();

      $data = array();

      while ( $Qcategories->next() ) {
//        $data[$Qcategories->valueInt('parent_id')][$Qcategories->valueInt('categories_id')] = array('name' => $Qcategories->value('categories_name'), 'image' => $Qcategories->value('categories_image'), 'count' => 0);
            $data[] = array('id' => $Qcategories->valueInt('categories_id'),
                           'text' => $Qcategories->value('categories_name'));        
//             array_push($data, $Qcategories->value('categories_name'));
      }

                  
//      $Qcategories->freeResult();

//      if ($this->_show_total_products === true) {
//        $this->_calculateProductTotals(false);
//      }
      return($data);
    }
    
    public static function xsell_delete_all($data, $default = false) {
      global $osC_Database, $osC_Language;

      $error = false;

      $osC_Database->startTransaction();
      
            $XsortList = $osC_Database->query('delete from :table_info_xsell where info_id = :info_idpost');
            $XsortList->bindTable(':table_info_xsell', TABLE_INFO_XSELL);
            $XsortList->bindInt(':info_idpost', $data['info_id']);
            $XsortList->execute();
            
        if ( $osC_Database->isError() ) {
          $error = true;
          break;
        }

      if ( $error === false ) {
        $osC_Database->commitTransaction();

        if ( $default === true ) {
          //osC_Cache::clear('configuration');
        }

        return true;
      }

      $osC_Database->rollbackTransaction();

      return false;
    }            

    public static function xsell_update($data_info, $data_xsell, $default = false) {
      global $osC_Database, $osC_Language;

      $error = false;

      $osC_Database->startTransaction();
      
           $XaddList = $osC_Database->query('insert into :table_info_xsell (ID, info_id, xsell_id, sort_order) values (:id, :info_id, :xsell_id, :xsell_sort)' );
           $XaddList->bindTable(':table_info_xsell', TABLE_INFO_XSELL);
           $XaddList->bindInt(':id', '');           
           $XaddList->bindInt(':info_id', $data_info);
           $XaddList->bindInt(':xsell_id', $data_xsell);
           $XaddList->bindInt(':xsell_sort', 1);      
           $XaddList->execute();
           
           $Number_addrow = 0;
           $Number_addrow = $XaddList->numberOfRows();
          
           if ( $Number_addrow >0 ) exit( $osC_Language->get('text_no_insert') );
            
        if ( $osC_Database->isError() ) {
          $error = true;
          break;
        }

      if ( $error === false ) {
        $osC_Database->commitTransaction();

        if ( $default === true ) {
          //osC_Cache::clear('configuration');
        }

        return true;
      }

      $osC_Database->rollbackTransaction();

      return false;
    } 
    
 }
?>
